package rss;

import com.alibaba.fastjson.JSONObject;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 获取rsshub所有分类
 * 获取分类下所有路由接口
 * 通过路由接口获取内容
 */
public class RssRoute {

    public static void main(String[] args) throws IOException, FeedException {
//        getClassify();
//        getRoute("bilibili");
//        getContent("bilibili/user/bangumi/208259");
        Map<String, Object> dataParams = new HashMap<>();
        dataParams.put("strBegTime", "strBegTime");
        dataParams.put("strEndTime", "strEndTime");
        System.out.println(JSONObject.toJSONString(dataParams));
    }

    public static void getRoute(String route) throws IOException {
        System.out.println(Jsoup.connect("http://localhost:1200/api/routes/" + route)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")
                .ignoreContentType(true)
                .header("charset","utf-8")
                .get().text());
    }

    public static void getClassify() throws IOException {
        String url = "https://docs.rsshub.app/";
        Connection connection = Jsoup.connect(url).timeout(3000)
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");
        Document document = connection.get();
        System.out.println(document);
        Elements elements = document.select("#app > div.theme-container > aside > ul > li:nth-child(2) > section > " +
                "ul");
        for (Element element : elements) {
            String link = element.getElementsByAttribute("a").text();
            System.out.println(link);
            System.out.println(url + link);
            Document doc = Jsoup.connect(url + link).timeout(3000)
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0")
                    .get();
            Elements elementsByClass = doc.getElementsByClass("sidebar-sub-headers");
            for (Element byClass : elementsByClass) {
                System.out.println(byClass.text());
            }
        }
    }

    public static List<Map> getContent(String xxx) throws IOException, FeedException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        URL url = new URL("http://127.0.0.1:1200/" + xxx);
        // 读取Rss源
        XmlReader reader = new XmlReader(url);
        System.out.println("Rss源的编码格式为：" + reader.getEncoding() + "\r");
        SyndFeedInput input = new SyndFeedInput();
        // 得到SyndFeed对象，即得到Rss源里的所有信息
        SyndFeed feed = input.build(reader);
        // 得到Rss新闻中子项列表
        List entries = feed.getEntries();
        // 循环得到每个子项信息
        for (int i = 0; i < entries.size(); i++) {
            // 标题、连接地址、标题简介、时间是一个Rss源项最基本的组成部分
            SyndEntry entry = (SyndEntry) entries.get(i);
            System.out.println("标题：" + entry.getTitle());
            System.out.println("连接地址：" + entry.getLink());
            SyndContent description = entry.getDescription();
            System.out.println("标题简介：" + description.getValue());
//            System.out.println("发布时间：" + df.format(entry.getPublishedDate()));
            // 以下是Rss源可先的几个部分
            System.out.println("标题的作者：" + entry.getAuthor());
            // 此标题所属的范畴
            List categoryList = entry.getCategories();
            if (categoryList != null) {
                for (int m = 0; m < categoryList.size(); m++) {
                    SyndCategory category = (SyndCategory) categoryList.get(m);
                    System.out.println("此标题所属的范畴：" + category.getName());
                }
            }
            // 得到流媒体播放文件的信息列表
            List enclosureList = entry.getEnclosures();
            if (enclosureList != null) {
                for (int n = 0; n < enclosureList.size(); n++) {
                    SyndEnclosure enclosure = (SyndEnclosure) enclosureList.get(n);
                    System.out.println("流媒体播放文件：" + enclosure);
                }
            }
            System.out.println();
        }
        return new LinkedList<>();
    }
}
